#!/bin/bash
function enable_local_yum(){
    echo -e "${blue}当前正在执行的操作为:${nocolor} ${green}配置本地YUM源${nocolor}"
    echo ""

 echo -ne "${blue}说明${nocolor}
* 启用本地YUM源仓库，则会禁用其他的YUM仓库
* 请在继续之前，将系统安装镜像，如CentOS-7-x86_64-DVD-1810.iso，
  拷贝到系统中或者直接挂载到光驱中
* 镜像版本需要和你的系统版本保持一致
继续[Yy,Enter],退出[Nn]: "
    read isYn
    [[ $isYn != [Nn] ]] || { sleep 0.5;return 1; }
    rm -rf /etc/yum.repos.d/CentOS-Local.repo
    echo ""

    isoLocation=
    echo -ne "请输入镜像文件所在目录，如果镜像已挂载在光驱中，则留空: "
    read -e isoLocation
    isoLocation=`echo $isoLocation`

    echo -e "$Info ${purple}挂载位置检查${nocolor}"
    if [ -n "$isoLocation" ];then
        isoName=`ls $isoLocation 2>>$logs_file|grep -E ".*\.iso$"|head -n 1`
        if [ -z "$isoName" ];then
          echo -e "$Error 给定的目录没有ISO镜像文件，退出"
          anykeyback
          return 1
        else
          source_point="$isoLocation/$isoName"
        fi
    else
        source_point="/dev/cdrom"
    fi
    
    echo -e "$Info ${purple}创建挂载点${nocolor}"
    if [ -w /mnt/ ];then
        repoDir=/mnt/LocalYum
        if [ -d /mnt/LocalYum ];then
          umount -f /mnt/LocalYum >/dev/null 2>&1
        else
          mkdir -p /mnt/LocalYum >/dev/null 2>&1
        fi
    else
        repoDir=/tmp/LocalYum
        if [ -d /tmp/LocalYum ];then
          umount -f /tmp/LocalYum >/dev/null 2>&1
        else
          mkdir -p /tmp/LocalYum >/dev/null 2>&1
        fi
    fi
    
    echo -e "$Info ${purple}挂载镜像文件${nocolor}"
    mount -o loop $source_point $repoDir
    
    Packages_dir=`find $repoDir -name "Packages" -type d|head -n 1`
    
    if [ -n "$Packages_dir" ];then
        echo -e "$Info ${purple}配置YUM源${nocolor}"
        echo -e "$Info ${purple}备份之前的仓库${nocolor}"
        for i in `ls /etc/yum.repos.d/*.repo 2>/dev/null`
        do
          /bin/mv -f $i $i.bak
        done
    
        cat > /etc/yum.repos.d/CentOS-Local.repo <<EOF
[CentOS-Local]
name=CentOS-$releasever - From Media
baseurl=file://$repoDir
enabled=1
gpgcheck=0
EOF
        yum clean all >/dev/null 2>&1
    else
        echo -e "$Error 挂载镜像到系统失败！！！"
        anykeyback
        return 1
    fi
    
    yum repolist |grep "CentOS-Local" >/dev/null 2>&1
    if [ "$?" == "0" ];then
        echo -e "$Info ${light_green}配置本地YUM仓库配置成功，赶快用yum命令安装软件试试吧！${nocolor}"
    fi      
    anykeyback
}

function disable_local_yum() {
    echo -ne "此操作会将/etc/yum.repos.d中.repo.bak文件还原，并删除\nCentOS-Local.repo文件\n继续[Yy,Enter],退出[Nn]: "
    read isYn
    [[ $isYn != [Nn] ]] || { sleep 0.5;return 1; }
    echo -e "$Info ${purple}还原repo.bak为repo${nocolor}"
    for i in `ls /etc/yum.repos.d/*.repo.bak 2>/dev/null`
    do
        /bin/mv -f $i ${i//.bak/}
    done
    rm -rf /etc/yum.repos.d/CentOS-Local.repo
    echo -e "$Info ${light_green}还原YUM源完成${nocolor}"
    anykeyback
}


function yum_options() {
    echo -e "${blue}选择要执行的操作${nocolor}
${green}1.${nocolor}启用本地YUM源
${green}2.${nocolor}停用本地YUM源
${green}3.${nocolor}退出"
    yumOptions=
    while [[ $yumOptions != [123] ]]
    do
      echo -ne "请选择操作: "
      read yumOptions
    done
    echo ""
    if [ "$yumOptions" == "1" ];then
        enable_local_yum
    elif [ "$yumOptions" == "2" ];then
        disable_local_yum   
    else
        sleep 0.10
        return 1
    fi
}



